package binaryTreeNode;

public class SortedArrayToBST {
    public static void main(String[] args) {
        int[] nums = {-10,-3,0,5,9};
        TreeNode root = sortedArrayToBST(nums);
        System.out.println(root.val);
    }
    public static TreeNode sortedArrayToBST(int[] nums) {

        return  insertIntoBST( nums,0,nums.length-1);


    }

    private static TreeNode insertIntoBST(int[] nums, int l,int r) {
        TreeNode root = new TreeNode();
        if (l>r)
            return null;
        root.val =  nums[((l+r)/2)];
        //TODO正确创建和连接节点
        root.left =insertIntoBST(nums,l,(l+r)/2-1);
        root.right= insertIntoBST(nums,(l+r)/2+1,r);
        return root;
    }
}
